package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author TieZhu
 * @since 2021-06-10
 */
@Api(tags = "积分等级管理")
@Slf4j
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;



    @GetMapping("/list")
    @ApiOperation(value = "查询积分表的数据列表")
    public R gradeList(){
//        log.info("hi i'm helen");
//        log.warn("warning!!!");
//        log.error("it's a error");
        List<IntegralGrade> integralGrades = integralGradeService.list();
        return R.ok().data("gradeList",integralGrades);
    }

    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "数据id", required = true, example = "100")
                            @PathVariable Long id){
        boolean res = integralGradeService.removeById(id);
        if (res){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "新增积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象",required = true)
                  @RequestBody IntegralGrade integralGrade){
        System.out.println(integralGrade.getBorrowAmount());
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean res=integralGradeService.save(integralGrade);
        if (res){
            return  R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据id查询积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id",required = true,example = "1")
            @PathVariable Long id){
        IntegralGrade grade = integralGradeService.getById(id);
        if (grade!=null){
            return R.ok().data("grade",grade);
        }else {
            return R.error().message("数据获取失败");
        }
    }
    @ApiOperation(value = "更新积分等级")
    @PutMapping("/update")
    public R updateById(@ApiParam(value = "积分等级对象",required = true)
                        @RequestBody IntegralGrade integralGrade){
        boolean res = integralGradeService.updateById(integralGrade);
        if (res){
            return R.ok().message("更新成功");
        }else {
            return R.error().message("更新失败");
        }

    }

}